/**
 * 员工职级管理
 */

app.controller('formSelfCtrl', ['$scope', '$http', FormSelfController]);
function FormSelfController($scope, $http){
	$scope.changeValueMapOfSwitch = function(field) {
		console.log('------status---', $scope.valueMap[field]);
		if (field == 'status') {//状态变更
//			$scope.valueMap[field] = ($scope.valueMap[field] == 0 ? 1 : 0);
			var statusVal = $scope.valueMap[field]; // 状态
			var userStateTxtVal = $scope.valueMap['userStateTxt'];
			if(statusVal==1){//停用
				if(userStateTxtVal!=undefined && userStateTxtVal!=null && userStateTxtVal!='' && userStateTxtVal!='离职'){
					$scope.showModalConfirm({
						tip: '该员工未离职是否确定停用？',
						ok: function() {
							$scope.valueMap[field] = 0;
							$scope.confirm.bsObj.hide();
						}
					});
				}
			} else {
				$scope.valueMap[field] = 1;
			}
		}
	}
	
	$scope.changeValueMap = function(field) {
		if(field == 'salaryStandard' || field == 'annualBonus'
			|| field == 'fixsalaryStandard' || field == 'socialSecurityByCompany' || field == 'medicalInsuranceByCompany'
			|| field == 'providentFundByCompany' || field == 'additionalCost' || field == 'yearEndBonus'){
			//年终奖/年 = 发薪标准/月*年终奖标准（月）
			var salaryStandard = parseFloat( $scope.valueMap['salaryStandard']); // 发薪标准/月
			var annualBonus = parseFloat($scope.valueMap['annualBonus']); // 年终奖标准（月）
			if((salaryStandard || salaryStandard==0) && (annualBonus || annualBonus==0)){
				var yearEndBonus = (salaryStandard*annualBonus).toFixed(2);
				$scope.valueMap['yearEndBonus'] = yearEndBonus;
			}

			//全年人力成本 = （定薪标准/月+社保（单位）/月+医保（单位）/月+公积金（单位）/月+附加费用）*12+年终奖/年
			var fixsalaryStandard = parseFloat($scope.valueMap['fixsalaryStandard']); // 定薪标准/月
			var socialSecurityByCompany = parseFloat($scope.valueMap['socialSecurityByCompany']); // 社保（单位）/月
			var medicalInsuranceByCompany = parseFloat($scope.valueMap['medicalInsuranceByCompany']); // 医保（单位）/月
			var providentFundByCompany = parseFloat($scope.valueMap['providentFundByCompany']); // 公积金（单位）/月
			var additionalCost = parseFloat($scope.valueMap['additionalCost']); // 附加费用
			var yearEndBonus = parseFloat($scope.valueMap['yearEndBonus']); // 年终奖/年
			if((fixsalaryStandard || fixsalaryStandard==0) && (socialSecurityByCompany || socialSecurityByCompany==0) && (medicalInsuranceByCompany || medicalInsuranceByCompany==0)
				&& (providentFundByCompany || providentFundByCompany==0) && (yearEndBonus || yearEndBonus==0)){
				var humanResourcesCost = ((fixsalaryStandard + socialSecurityByCompany + medicalInsuranceByCompany
					+ providentFundByCompany ) * 12
					+ yearEndBonus).toFixed(2);
				$scope.valueMap['humanResourcesCost'] = humanResourcesCost;
			}
			if((fixsalaryStandard || fixsalaryStandard==0) && (socialSecurityByCompany || socialSecurityByCompany==0) && (medicalInsuranceByCompany || medicalInsuranceByCompany==0)
				&& (providentFundByCompany || providentFundByCompany==0) && (additionalCost || additionalCost==0) && (yearEndBonus || yearEndBonus==0)){
				var humanResourcesCost = ((fixsalaryStandard + socialSecurityByCompany + medicalInsuranceByCompany
					+ providentFundByCompany + additionalCost) * 12
					+ yearEndBonus).toFixed(2);
				$scope.valueMap['humanResourcesCost'] = humanResourcesCost;
			}

		}
	}

	$scope.doClickOnItem = function(field) {
		console.log('------field---', field);
		if(field == 'dingDingId'){

			var telephone = $scope.valueMap['telephone']; // 联系电话
			var userId = $scope.valueMap['userId']; // 工号
			if(userId==undefined || userId==null || userId==''){
				layer.msg("请先选择用户！");
				return;
			}
			layer.open({
				shadeClose:true,
				title: '填入钉钉中设置的手机号',
				content: `
								<form class="layui-form">
								  <div class="layui-form-item">
									 <input type="text" class="layui-input" id="inputTelephone" value="`+telephone+`" placeholder="填入钉钉中设置的手机号">
								  </div>
								</form>
							`,
				success: function(layero, index) {},
				yes: function (index, layero) {
					var inputTelephone = document.getElementById("inputTelephone").value;
					if(inputTelephone==undefined || inputTelephone==null || inputTelephone==''){
						layer.msg("请输入您在钉钉设置的手机号！");
						return;
					}
					$http.post('/api/inter/tbUserAttendance/getDingDingUserIdByPhone', {}, {params: {userId:userId,telephone:inputTelephone}})
						.success(function (data) {
							if (data.code == 0) {
								$scope.valueMap['dingDingId'] = data.msg;
								layer.msg('成功！');
								setTimeout(function(){
									layer.closeAll();
								}, 1000);
							} else {
								layer.msg(data.msg);
								layer.closeAll();
							}
						})
						.error(function(result){
							layer.close();
							layer.msg("操作失败，请稍后再试！", {icon: 2});
						});

				}
			});
		}
	}
}